/*===============================================================================
	REPLICATION FILE FOR
		"No Surprises, Please: Voting Costs and Electoral Turnout",
			by J.-V. Alipour and V. Lindlacher
			Journal of Political Economy Microeconomics
		
	Version: 3 MARCH 2024
	
	Instructions:
		You only need to specify the directory of the project folder
			in section 1 (line 33).
		Then simply run this script to replicate the analysis.	
		
					


*================================================================================*/

cap log close
clear all
set more off
set varabbrev off
set seed 123

version 17


********************************************************************************
* 1. Settings [YOUR ACTION NEEDED HERE]
********************************************************************************

*** [1] Replace "..." with the directory to the replication folder
global  project	"..."


*** [2] Optional: set the globals to 1 if you want to run your own geocoding;
* 		 -> in this case you need to provide your OpenCageGeo API key	

// Geocodeing of voters' addresses	
global RERUN_GEOCODE_VOTER=0
// Geocoding of polling places
global RERUN_GEOCODE_PP=0
// Your API Key
global api_key "..."


********************************************************************************
* 2. Set paths relative to project directory
********************************************************************************
/* Scripts */
global 	do_path 	"$project/01_code"				// DOfiles, organized in subfolders stored here

/* Data folders */
global 	rawdata 	"$project/02_rawdata"			 // raw data, organized in subfolders
global 	geocod		"$project/03_geocoded_addresses" // Folder that contains the outcome of georeferencing (preprocessed)
global 	shp_path	"$project/02_rawdata/shp"		 // Folder that contains shapefiles
global  arc_anly	"$project/06_arc_analyze"		 // Our output from GIS calculations is stored here
global  newdata		"$project/04_newdata"			 // new generated files used for estimation and descriptives are saved here
global 	tmp 		"$project/tmp"					 // new generated auxiliary files are saved here

/* Output Folders */
global 	tables		"$project/07_output/tables"		// Output: tables
global 	figures		"$project/07_output/figures"	// Output: figures
global 	logs		"$project/07_output/logs"		// Output: log files


cd "$project"

log using "$logs/master.log", replace

********************************************************************************
* 3. Programs and ados
********************************************************************************
* Change ado path to packages folder for this session
sysdir set PLUS 	"$project/05_packages/ados/plus" // external ados
adopath ++ PLUS 	

* Run author's programs
run "$project/05_packages/00_programs.do"




********************************************************************************
* 4. Set figure presettings
********************************************************************************

grstyle clear
set scheme plotplain
grstyle init mygr , path("`c(sysdir_plus)'") replace
grstyle set plain, horizontal noextend
grstyle set lpattern solid: p2lineplot
grstyle set legend 12, nobox klength(5)
grstyle set symbol Oh Oh
grstyle set color black: histogram_line axisline tick
grstyle set color black%25: major_grid
grstyle set color black%80  maroon%80 olive edkblue 
set scheme mygr


********************************************************************************
* 5. Set presettings for estimation
********************************************************************************
	// set global for recurring set controls					
	global ctr  ln_ew_ges ew_biodt ew_dtmihi ew_ledig ew_married wb_anteil ///
				 wb_ausl wb_18t24 wb_25t34 wb_35t44 wb_45t59 avg_dur hh_kids mpreis_flats_rent
			
	// set basic outreg options (with 2 or 3 decimals)
	global opt 	se starlev(5 1 .1) nocons noleg starloc(1) summstat(r2 \ N )  statfont(fs32 fnew1) sdec(2) varlabels merge	

	global opt3 se starlev(5 1 .1) nocons noleg starloc(1) summstat(r2 \ N )  statfont(fs32 fnew1) sdec(3) varlabels merge

	// set regression weights: #eligible voters
	global wgt [aw=wahlber_gesamt]

********************************************************************************
* 6. Run Scripts
********************************************************************************

set graphics off
log close

*** 1. Prep addresses and geocode locations
	log using "$logs/1_prep_and_geolocate_addresses.log", replace
	
	// list dofiles in alphabetical order (saved in local files)
	local files : dir "$do_path/1_prep_and_geolocate_addresses/" files "*.do"
	local files: list sort files
	macro list _files
	// run dofiles
	foreach f of local files {
		di "Running: `f'"
		do "$do_path/1_prep_and_geolocate_addresses/`f'"
	}
	log close 

*** 2. Build datasets
	log using "$logs/2_build_datasets.log", replace
	
	// list dofiles in alphabetical order (saved in local files) 
	local files : dir "$do_path/2_build_datasets/" files "*.do"
	local files: list sort files
	macro list _files
	// run dofiles
	foreach f of local files {
		di "Running: `f'"
		do "$do_path/2_build_datasets/`f'"
	}
	 
	
	/* erase auxiliary files from tmp folder */
	local files : dir "$tmp/" files "*.dta"
	foreach f of local files {
		erase "$tmp/`f'"
	}
	log close

*** 3. Run Analysis
	log using "$logs/3_analysis.log", replace
	
	// list dofiles in alphabetical order (saved in local files) 
	local files : dir "$do_path/3_analysis/" files "*.do"
	local files: list sort files
	macro list _files
	
	// run dofiles
	foreach f of local files {
		di as text "Running: `f'"
		do "$do_path/3_analysis/`f'"
	}
	log close 
clear




	
	
	
	
	






